﻿using Com.JunCaiSoft.Common.Controller;
using Com.JunCaiSoft.ConstructionCashFlow.Entity;
using Com.JunCaiSoft.ConstructionCashFlow.IDao;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Com.JunCaiSoft.API.ConstructionCashFlow.Controllers
{
    /// <summary>
    /// 工程类型
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ProjectTypeController : BaseController<ProjectType,ProjectType,ProjectType,String>
    {
        public ProjectTypeController(IProjectTypeDao projecttypedao) : base(projecttypedao)
        {

        }
        /// <summary>
        /// 创建工程类型
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public override async Task<ActionResult> CreateAsync(ProjectType entity)
        {
            try
            {
                var existsone = await DaoAsync.GetAsync(x => x.Name == entity.Name);
                if (existsone == null)
                {
                    return await base.CreateAsync(entity);
                }
                else
                {
                    return await Task.Run(() => Content(JsonConvert.SerializeObject(0)));
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}
